package jp.eisbahn.mgapi.api.people.lookup;

import java.io.InputStream;
import java.util.Map;

import jp.eisbahn.mgapi.api.AbstractPagingRequest;
import jp.eisbahn.mgapi.api.Response;
import jp.eisbahn.mgapi.api.people.PeopleResponse;
import jp.eisbahn.mgapi.http.ContentType;
import jp.eisbahn.mgapi.http.HttpMethod;
import jp.eisbahn.mgapi.http.HttpUtils;
import jp.eisbahn.mgapi.util.StringUtils;

/**
 * The request class to retrieve the people information using e-mail addresses
 * with People lookup API.
 * @author Yoichiro Tanaka
 */
public class PeopleLookupRequest extends AbstractPagingRequest {

    /** The base URI string. */
    private static final String BASE_URI = "/search/people?";

    /** E-mail addresses. */
    private String[] emails;

    /** The fields. */
    private String[] fields;

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getUri()
     */
    @Override
    public final String getUri() {
        String joined = StringUtils.join(emails, ",");
        String[] fieldsParam = null;
        if (fields != null && fields.length > 0) {
            fieldsParam = new String[] {
                    "fields", StringUtils.join(fields, ",")};
        }
        String queryParameterString =
            HttpUtils.createQueryParameterString(
                    new String[] {"q", joined},
                    getStartIndexQueryParameter(),
                    getCountQueryParameter(),
                    fieldsParam
            );
        return BASE_URI + queryParameterString;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getHttpMethod()
     */
    @Override
    public final HttpMethod getHttpMethod() {
        return HttpMethod.GET;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getResponseClass()
     */
    @Override
    public final Class<? extends Response> getResponseClass() {
        return PeopleResponse.class;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getContentType()
     */
    @Override
    public final ContentType getContentType() {
        return ContentType.X_WWW_FORM_URLENCODED;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getBodyStream()
     */
    @Override
    public final InputStream getBodyStream() {
        return null;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getParameterMap()
     */
    @Override
    public final Map<String, Object> getParameterMap() {
        return null;
    }

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Request#getHeaderMap()
     */
    @Override
    public final Map<String, String> getHeaderMap() {
        return null;
    }

    /**
     * Set e-mail addresses.
     * @param theEmails E-mail address array of String.
     */
    public final void setEmails(final String[] theEmails) {
        emails = theEmails;
    }

    /**
     * Retrieve e-mail addresses.
     * @return E-mail addresses array of String.
     */
    public final String[] getEmails() {
        return emails;
    }

    /**
     * Retrieve the fields value.
     * @return The fields value.
     */
    public final String[] getFields() {
        return fields;
    }

    /**
     * Set a fields value.
     * @param newFields The fields to set
     */
    public final void setFields(final String[] newFields) {
        this.fields = newFields;
    }

}
